

*******************************************************************************************************************************************************
*PREDICTING VALUE OF OBSERVABLES:
*		The following code has been modified in order to use a different grouping strategy grouping, 
*		using quartiles instead of terciles
*		Also, the following code has been modified in order to correct the coding error.
*******************************************************************************************************************************************************
***********************************************************
*STEP 1- CLEAN CONTROLS FOR LASSO
***********************************************************	
	local controls $controls
		
	*PDS LASSO CODE

		cap drop _*
		cap drop *AA* 
		
		local k=1
		if "`controls'"!="" {
		foreach var of varlist `controls' {
			g AA`k'= `var'
				local ++k
			}
		}

		unab AA: AA*
			lassoClean `AA'
			ds _AA*
		
		foreach var of varlist _AA*{
			sum `var'
			gen miss_`var'=`var'==.
			replace `var'=r(mean) if miss_`var'==1
			gen Winner_`var'=Winner*`var'
			gen mi_Winner_`var'=Winner*miss_`var'
			}


***********************************************************
*STEP 2- RUN PDS LASSO FIR JUST CONTROLS
*grab the controls that pds lasso selects
***********************************************************		
	local m=1
	
	foreach out in Trim_Income log_Income Trim_Profits_30Days log_Profits {
		/// The order of the variables in the previous line of code was modified to match the order of the variables at line 111.

		
	* run lasso
	pdslasso `out' Winner (Winner__AA* mi_Winner__AA* i.Surveyor_Code i.Survey_Version ///
		i.survey_month ) [weight=Propensity_Score] if  Survey_Version!=5 &  Done==1, ///
		partial(i.Surveyor_Code i.Survey_Version i.survey_month ) cluster(GroupNumber) fe
			
	*store selected variables
	global selected  `e(xselected_chs_l)'	
	
	gen MR_Prediction_Obs`m'=_b[Winner]

	foreach var of global selected {
		local variable=substr("`var'",8,20)
		display "`variable'"
		replace MR_Prediction_Obs`m'= MR_Prediction_Obs`m'+_b[`var']*`variable'
	}	
	
	tempvar rank_quartile
		egen `rank_quartile'=cut(MR_Prediction_Obs`m'), group(4)
		tab `rank_quartile', gen(MR_Prediction`m'_Obs_quartile_)
		gen Winner_MR_Prediction`m'_Obs_Q2= Winner*MR_Prediction`m'_Obs_quartile_2
		gen Winner_MR_Prediction`m'_Obs_Q3= Winner*MR_Prediction`m'_Obs_quartile_3
		gen Winner_MR_Prediction`m'_Obs_Q4= Winner*MR_Prediction`m'_Obs_quartile_4

***********************************************************
*STEP 3- NOW ADD RANK TO PREDICTION
*grab the controls that pds lasso selects
***********************************************************		
	
	* run lasso
	pdslasso `out'  Winner (Winner_Quint_Rank_NS Winner__AA* mi_Winner__AA*   i.Surveyor_Code i.Survey_Version ///
		i.survey_month ) [weight=Propensity_Score] if  Survey_Version!=5 &  Done==1, ///
		partial(i.Surveyor_Code i.Survey_Version i.survey_month ) cluster(GroupNumber) fe
		
		
	*store selected variables
	global selected  `e(xselected_chs_l)'	
	
	gen _Quint_Rank_NS`m'=Quint_Rank_NS
	gen MR_Prediction_Obs_Rank`m'=_b[Winner]
	local i=1 
	foreach var of global selected {
		local variable=substr("`var'",8,20)
		display "`variable'"
		replace MR_Prediction_Obs_Rank`m'= MR_Prediction_Obs_Rank`m'+_b[`var']*`variable'
	}	
	
	tempvar rank_quartile
		egen `rank_quartile'=cut(MR_Prediction_Obs_Rank`m'), group(4)
		tab `rank_quartile', gen(MR_Prediction`m'_Obs_Rank_Q_)
		gen Winner_MR_Predict`m'_Obs_Rank_Q2= Winner*MR_Prediction`m'_Obs_Rank_Q_2
		gen Winner_MR_Predict`m'_Obs_Rank_Q3= Winner*MR_Prediction`m'_Obs_Rank_Q_3	
		gen Winner_MR_Predict`m'_Obs_Rank_Q4= Winner*MR_Prediction`m'_Obs_Rank_Q_4	

	local m=`m'+1
	}

***********************************************************
*STEP 4- REGRESSION
***********************************************************			
			
	
	maketable_t4_2     Trim_Income log_Income   Trim_Profits_30Days log_Profits   if  Survey_Version!=5 &  Done==1 & Winner_Quint_Rank_NS!=. , /// 
	table("${outtex}${date}_MR_Predict_Obs_Rank_grouping.tex") ///
	type(tex) /// 
	footnote($footnote_t4_grouping) ///
	prehead1($prehead1) prehead2($prehead2) /// 
	 posthead("\hline") ///
	title("Replicating Table 4 in Hussam et al. (2022b): Grouping Predictions in Quartiles instead of Terciles") label(predictobservablesrank_grouping)
